import { Buffer } from 'buffer';

console.log('--------- 实例1 ---------')
const bufA01 = Buffer.from('ABC');
const bufA02 = Buffer.from('ABC');
const bufA03 = Buffer.from('ABCD');
console.log(bufA01.equals(bufA02));//true
console.log(bufA01.equals(bufA03));//false


console.log('--------- 实例2 ---------')
const bufB01 = Buffer.from('aabbcc');
console.log(bufB01.includes('bb'));//true
console.log(bufB01.indexOf('bb'));//2
console.log(bufB01.lastIndexOf('bb'));//2

console.log('--------- 实例3 ---------')
const bufD01 = Buffer.allocUnsafe(3);
bufD01.fill('h');
console.log(bufD01.toString());//hhh

console.log('--------- 实例4 ---------')
const bufE01 = Buffer.from('ABCDEF');
const bufE02 = Buffer.allocUnsafe(8).fill('!');
bufE01.copy(bufE02, 1, 3, 5);
console.log(bufE01.toString());//ABCDEF
console.log(bufE02.toString());//!DE!!!!!

console.log('--------- 实例5 ---------')
const bufF01 = Buffer.from('ABCDEF');
const bufF02 = bufF01.subarray(1, 3);
const bufF03 = bufF01.slice(1, 3);
console.log(bufF02.toString());//BC
console.log(bufF03.toString());//BC

console.log('--------- 实例6 ---------')
const bufG01 = Buffer.alloc(32);
bufG01.write('abcd');
console.log(bufG01.toString());//abcd


console.log('--------- 迭代 ---------')
const bufC01 = Buffer.from('ABC');
for (const key of bufC01.keys()) {
	console.log(key, bufC01[key]);
	//0 65
	//1 66
	//2 67
}
for (const key of bufC01.values()) {
	console.log(key);
	//65
	//66
	//67
}
for (const key of bufC01.entries()) {
	console.log(key);
	//[0 65]
	//[1 66]
	//[2 67]
}
